require(['./config'], () => {
    require(['jquery', 'cookie'], () => {
        class Register {
            constructor() {
                this.init();
            }
            init() {
                console.log($.cookie('info'))
                var regex = /^[-_A-Za-z0-9]+@([_A-Za-z0-9]+\.)+[A-Za-z0-9]{2,3}$/; //判断邮箱
                var regex2 = /^(\w){6,20}$/;
                $('button').on('click', () => {
                    var mail = $('.mail').val(),
                        username = $('.username').val(),
                        password = $('.password').val();
                    if (mail && username && password) { //判读输入是否为空
                        var flag1 = regex.test(mail) //判断邮箱
                        var flag2 = regex2.test(password)
                        if (flag1 && flag2) {
                            let info = $.cookie('info')
                            if (info) {
                                info = JSON.parse(info)
                                var isEx = info.some(user => {
                                    return user.mail === mail
                                })
                                if (isEx) {
                                    $('.mail').val('');
                                    $('.username').val('');
                                    $('.password').val('');
                                    alert('该邮箱已注册');
                                } else {
                                    info.push({
                                        mail,
                                        username,
                                        password
                                    })
                                    info = JSON.stringify(info)
                                    $.cookie('info', info, {
                                        expires: 365,
                                        path: '/'
                                    });
                                    alert('注册成功');
                                    $('.mail').val('');
                                    $('.username').val('');
                                    $('.password').val('');
                                }
                            } else {
                                var arr = []
                                arr.push({
                                    mail,
                                    username,
                                    password
                                })
                                info = JSON.stringify(arr)
                                $.cookie('info', info, {
                                    expires: 365,
                                    path: '/'
                                });
                                alert('注册成功')
                                $('.mail').val('');
                                $('.username').val('');
                                $('.password').val('');
                            }
                        } else {
                            $('.mail').val('');
                            $('.username').val('');
                            $('.password').val('');
                            alert('请输入正确的邮箱地址或密码');
                        }
                    } else {
                        alert('信息栏不能为空');
                    }
                })

            }
        }
        new Register()
    })
})